/**
 * 
 */
function pageClick(pagnow) {
	pageNow = Number(pagnow);
	window.getTopic();
}
function pageNextClick() {
	pagnow += 1;
	window.getTopic();
}
function labelClick(label_id) {
	label = label_id;
	window.getTopic();
}
function stateClick(state_id) {
	state = state_id;
	window.getTopic();
}
jQuery(function($) {
	$(window.getTopic = function() {
		$
				.ajax({
					type : "post",
					url : "GetBBSTopicByUIDServlet",
					data : {
						user_id : user_id,
						state : state,
						pageNow : pageNow,
						pageNum : 10
					},
					dateType : "json",
					success : function(data) {
						var jsondata = eval('(' + data + ')');
						var contentdiv, newContentdivNode, newBaseImfoNode, newPageNodeup, newPageNodedown, newLabelNode, newTopicContentNode, newTCNode, newTopicTop;
						contentdiv = $("#ct");
						newContentdivNode = $("<div></div>").addClass("mn");
						// 加载 forum base imfo done
						newBaseImfoNode = setForumBaseImfo(jsondata);
						newContentdivNode.append(newBaseImfoNode);
						// 页面工具加载 done
						newPageNodeup = setPagesTools(jsondata.pagenum,
								jsondata.pagenow);
						newContentdivNode.append(newPageNodeup);
						// topic list
						newTopicContentNode = $("<div></div>").addClass(
								"tl bm bmw").attr("id", "threadlist");
						// topic top
						newTopicTop = setTopicTop(jsondata.topicstate);
						newTopicContentNode.append(newTopicTop);

						newTopicContentNode
								.append("<div class=\"bm_c\"><table id=\"threadlisttableid\" cellspacing=\"0\" cellpadding=\"0\" summary=\"forum_27\"></table></div>");
						// TODO load topic
						setTopicList(jsondata.topic, newTopicContentNode
								.find("#threadlisttableid"));
						newContentdivNode.append(newTopicContentNode);

						// TODO load 页面工具
						if (jsondata.pagenum != 1 && jsondata.pagenum != 0) {
							newContentdivNode
									.append("<a id=\"autopbn\" class=\"bm_h\" forumdefstyle=\"\" picstyle=\"0\" totalpage=\"203\" curpage=\"1\" onclick=\"pageNextClick()\" href=\"javascript:;\">下一页 </a>");
						}
						newPageNodedown = setPagesTools(jsondata.pagenum,
								jsondata.pagenow);
						newContentdivNode.append(newPageNodedown);

						contentdiv.html(newContentdivNode);
					},
					error : function() {
						// TODO do something
					}
				});
	});
	// 基本信息
	function setForumBaseImfo(jsondata) {
		var newBaseImfoNode, newNoticeNode;
		$("#pathdiv")
				.html(
						"<a href=\"../community/community.jsp\" target=\"_top\" class=\"nvhm\" title=\"首页\">协作社区</a><em>»</em><a href=\"../community/community.jsp\" target=\"_top\" >社区</a>"
								+ "<em>›</em>" + "<a href=\"#\">我的帖子</a>");
		// forum base imfo
		newBaseImfoNode = $("<div></div>").addClass("bm bml pbn");
		// 专题基本信息
		newBaseImfoNode.append("<div class=\"bm_h cl\"> "
				+ "<span class=\"o\"> </span>"
				+ "<h1 class=\"xs2\"> <a href=\"#\">&nbsp;" + "我的帖子" + "</a>"
				+ "</h1> </div>");
		// 专题公告及版主
		newNoticeNode = $("<div></div>").addClass("bm_c cl pbn").attr("id",
				"forum_rules");
		// TODO username's key 需要修改 点击方法或地址未填写
		newBaseImfoNode.append(newNoticeNode);
		return newBaseImfoNode;
	}
	// 页管理
	function setPagesTools(pageCount, pageNow) {
		var pageTools, pagelistNode, newPublicNode;
		pagelistNode = $("<span></span>").attr("id", "fd_page_top");
		pagelistNode.append("<div class=\"pg\"></div>");
		for (var i = 1; i < pageCount; i++) {
			if (i == pageNow) {
				pagelistNode.children().append("<strong>" + i + "</strong>");
			} else if (i == pageCount) {
				// 第i页点击事件
				pagelistNode.children()
						.append(
								"<a class=\"last\" onclick=\"pageClick('" + i
										+ "')\" href=\"javascript:;\">..." + i
										+ "</a>");
			} else {
				// 第i页点击事件
				pagelistNode.children().append(
						"<a onclick=\"pageClick('" + i
								+ "')\" href=\"javascript:;\">" + i + "</a>");
			}
		}
		pagelistNode
				.children()
				.append(
						"<label> <input class=\"px\" type=\"text\" onkeydown=\"if(event.keyCode==13) {}\" value=\"1\" title=\"输入页码，按回车快速跳转\" size=\"2\" name=\"custompage\"> <span title=\"共 "
								+ pageCount
								+ "页\"> // "
								+ pageCount
								+ " 页</span> </label>");
		// TODO 下一页点击事件
		pagelistNode.children().append("<a class=\"nxt\" href=\"\">下一页</a>");
		// TODO 高级发布点击事件
		newPublicNode = $("<a></a>").attr("id", "newspecial").attr("href",
				"../community/forum_edit.html");
		newPublicNode.append("<img alt=\"发新帖\" src=\"../images/pn_post.png\">");

		pageTools = $("<div></div>").addClass("bm bw0 pgs cl")
				.attr("id", "pgt");
		if (pageCount != "1" && pageCount != "0") {
			pageTools.append(pagelistNode);
		}
		pageTools.append(newPublicNode);
		return pageTools;
	}

	// Topic title
	function setTopicTop(topicStates) {
		var topicTop, newTableNode, newTbodyNode, newTrNode;
		topicTop = $("<div></div>").addClass("th");
		topicTop
				.append("<table cellspacing=\"0\" cellpadding=\"0\">"
						+ "<tbody><tr>"
						+ "<th colspan=\"2\">"
						+ "<div id=\"topicstate\" class=\"tf\">"
						+ "</th>" + "<td class=\"by\">作者</td>"
						+ "<td class=\"num\">回复/查看</td>"
						+ "<td class=\"by\">最后发表</td>"
						+ "</tr></tbody></table>");
		// TODO 取消状态限制标签
		topicTop
				.find("#topicstate")
				.append(
						"<a class=\"xi2\" onclick=\"stateClick('-1')\" href=\"javascript:;\" >全部</a>");
		for (var i = 0; i < topicStates.length; i++) {
			// TODO 添加状态限制
			topicTop.find("#topicstate").append(
					"<a class=\"xi2\" onclick=\"stateClick('"
							+ topicStates[i].Name
							+ "')\" href=\"javascript:;\" >&nbsp;"
							+ topicStates[i].Name + "&nbsp;</a>");
		}
		topicTop
				.find("#topicstate")
				.append(
						"<span id=\"clearstickthread\" style=\"display: none;\">"
								+ "<span class=\"pipe\">|</span>"
								+ "<a class=\"xi2\" title=\"显示置顶\" onclick=\"clearStickThread()\" href=\"javascript:;\">显示置顶</a>"
								+ "</span>");
		return topicTop;
	}
	// hot topic table
	function setHotTopicList(topicArray, tableNode) {
		if (topicArray.length == 0) {
			return false;
		}
		var hotTopicNode;
		for (var i = 0; i < topicArray.length; i++) {
			hotTopicNode = $("<tbody></tbody").attr("id", "stickthread_" + i);
			hotTopicNode.append("<tr></tr>");
			// TODO 打开主题界面
			hotTopicNode.children().append(
					"<td class=\"icn\">"
							+ "<a target=\"_blank\" title=\"置顶主题\" >"
							+ "<img alt=\"置顶\" src=\"../images/pin_3.gif\">"
							+ "</a></td>");
			// TODO 打开主题界面
			hotTopicNode
					.children()
					.append(
							"<th class=\"common\">"
									+ "<a class=\"showhide y\" title=\"隐藏置顶帖\" onclick=\"hideStickThread('"
									+ i
									+ "')\" href=\"javascript:void(0);\">隐藏置顶帖</a>"
									+ "<a class=\"s xst\" onclick=\"atarget(this)\" style=\"font-weight: bold; color: #EE1B2E;\" href=\"\">"
									+ topicArray[i].Title + "</a>" + "</th>");
			// TODO 打开用户个人界面
			hotTopicNode
					.children()
					.append(
							"<td class=\"by\">"
									+ "<cite>"
									+ "<a style=\"color: red;\" c=\"1\"  mid=\"card_415\">"
									+ topicArray[i].CreatorName + "</a>"
									+ "</cite>" + "<em>"
									+ "<span><span title=\""
									+ topicArray[i].CreatedOn + "\">"
									+ topicArray[i].CreatedOn
									+ "</span></span>" + "</em></td>");
			// TODO 打开主题界面
			hotTopicNode.children().append(
					"<td class=\"num\">" + "<a class=\"xi2\">"
							+ topicArray[i].ReplyCount + "</a><em>"
							+ topicArray[i].Hit + "</em></td>");
			// TODO 打开用户个人界面
			var rname = '', ron = '';
			if (topicArray[i].RepliedName) {
				rname = topicArray[i].RepliedName
				ron = topicArray[i].RepliedOn
			}
			hotTopicNode.children().append(
					"<td class=\"by\">" + "<cite><a c=\"1\" mid=\"card_1220\">"
							+ rname + "</a></cite>" + "<em><a>"
							+ "<span title=\"" + ron + "\">" + ron + "</span>"
							+ "</a></em></td>");
			tableNode.append(hotTopicNode);
		}
	}
	// topic table
	function setTopicList(topicArray, tableNode) {
		if (topicArray.length == 0) {
			return false;
		}
		var topicNode;
		for (var i = 0; i < topicArray.length; i++) {
			topicNode = $("<tbody></tbody>").attr("id", "normalthread_" + i);
			topicNode.append("<tr></tr>");
			topicNode.children().append(
					"<td class=\"icn\">"
							+ "<a target=\"_blank\" title=\"\" href=\"\">"
							+ "<img src=\"../images/folder_new.gif\">"
							+ "</a></td>");
			topicNode
					.children()
					.append(
							"<th class=\"new\">"
									+ "<a id=\"content_"
									+ topicArray[i].ID
									+ "\" class=\"showcontent y\" title=\"更多操作\"><label for=\"manage5744487\"><input id=\"manage_"
									+ topicArray[i].ID
									+ "\" name=\"chk\" class=\"pc\" type=\"checkbox\" autocomplete=\"off\" value=\""
									+ topicArray[i].ID
									+ "\" onclick=\"showTools()\""
									+ topicArray[i].ID
									+ "\" >管理</label></a>"
									+ "<a class=\"s xst\" href=\"../community/forum_topic.html?topic={id:"
									+ topicArray[i].ID + "}\" >"
									+ topicArray[i].Title + "</a>" + "</th>");
			topicNode
					.children()
					.append(
							"<td class=\"by\">"
									+ "<cite>"
									+ "<a style=\"color: red;\" c=\"1\"  mid=\"card_415\">"
									+ topicArray[i].CreatorName + "</a>"
									+ "</cite>" + "<em>"
									+ "<span><span title=\""
									+ topicArray[i].CreatedOn + "\">"
									+ topicArray[i].CreatedOn
									+ "</span></span>" + "</em></td>");
			topicNode
					.children()
					.append(
							"<td class=\"num\">"
									+ "<a class=\"xi2\" href=\"../community/forum_topic.html?topic={id:"
									+ topicArray[i].ID + "}\">"
									+ topicArray[i].ReplyCount + "</a><em>"
									+ topicArray[i].Hit + "</em></td>");
			var rname = '', ron = '';
			if (topicArray[i].RepliedName) {
				rname = topicArray[i].RepliedName
				ron = topicArray[i].RepliedOn
			}
			topicNode
					.children()
					.append(
							"<td class=\"by\">"
									+ "<cite><a c=\"1\" mid=\"card_1220\">"
									+ rname
									+ "</a></cite>"
									+ "<em><a href=\"../community/forum_topic.html?topic={id:"
									+ topicArray[i].ID + "}\">"
									+ "<span title=\"" + ron + "\">" + ron
									+ "</span>" + "</a></em></td>");
			tableNode.append(topicNode);
		}
	}
});